fix(user-event): include selection in TextInput change event#1919
Open
nsbarsukov wants to merge 1 commit into
Open
fix(user-event): include selection in TextInput change event#1919nsbarsukov wants to merge 1 commit into
selection in TextInput change event#1919nsbarsukov wants to merge 1 commit into
Conversation
React Native (>=0.85) reports caret position via onChange's nativeEvent.selection on all platforms, but userEvent.type only emitted it on a separate selectionChange event. Components that read the caret inside onChange (e.g. input masks) could not be driven by userEvent. Fold the computed selectionRange into the change event.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
React Native (>=0.85) reports the caret position via
onChange'snativeEvent.selection(see commits from react-native repo162627aandc1f5445).userEvent.type()did not includeselectionin thechangeevent'snativeEvent— it computed aselectionRange(caret at the end of the current text) and emitted it only via a separateselectionChangeevent. As a result, components that read the caret fromevent.nativeEvent.selectioninside theironChangehandler (e.g. input-masking libraries) could not be driven byuserEvent.type(...): they saw no selection, mishandled every keystroke, and the value never updated.This PR folds the already-computed
selectionRangeinto thechangeevent so it matches the real platform shape, while keeping the separateselectionChangeevent unchanged.Changes:
buildTextChangeEvent(text, selection)now includesselection: { start, end }in the change event'snativeEvent.type()(emitTypingEvents) andpaste()pass the caret range (end of the current/inserted text) into the change event.clear()inherits this viaemitTypingEvents.submitEditing/endEditingare left as-is — the platform does not report a selection on those events.Test plan
yarn test— all suites pass (snapshots fortype,type-managed,paste, andclearregenerated to includeselection).yarn typecheck,yarn lint,yarn format:check— all pass.src/user-event/type/__tests__/type.test.tsx:userEvent.type(input, 'Hello'), theonChangehandler receives an event whosenativeEvent.selectionequals{ start: 5, end: 5 }.TextInputthat readse.nativeEvent.selectioninonChangeand only updates its value when the caret is present — confirms it is now correctly driven byuserEvent.type().src/event-builder/__tests__/text.test.tsto assertselectionis present in the built change-event payload.